package com.artevivo.utilidades;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * The Class Log4JavaServiceImpl.
 */
public class Log4JavaServiceImpl implements ILogService {

   /** The logger. */
   private Logger logger = Logger.getLogger(Log4JavaServiceImpl.class);

   /**
    * Instantiates a new log4 java service impl.
    * 
    * @param logger
    *           the logger
    */
   public Log4JavaServiceImpl(Logger logger) {
      super();
      this.logger = logger;
   }

   /**
    * Instantiates a new log4 java service impl.
    */
   public Log4JavaServiceImpl() {
      super();
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.psiject.utilidades.ILogService#crearLog(java.lang.String,
    *      java.lang.Throwable)
    */
   public ILogData crearLog(String mensaje, Throwable e) {
      crearLog(mensaje, false, ILogData.ERROR, -1, "", "", e);
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.psiject.utilidades.ILogService#crearLog(java.lang.String,
    *      boolean, java.lang.Throwable)
    */
   public ILogData crearLog(String mensaje, boolean resBundle, Throwable e) {
      crearLog(mensaje, resBundle, ILogData.ERROR, -1, "", "", e);
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.psiject.utilidades.ILogService#crearLog(java.lang.String,
    *      boolean, int, java.lang.Throwable)
    */
   public ILogData crearLog(String mensaje, boolean resBundle, int nivel, Throwable e) {
      crearLog(mensaje, resBundle, nivel, -1, "", "", e);
      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.psiject.utilidades.ILogService#crearLog(java.lang.String,
    *      boolean, int, int, java.lang.String, java.lang.String,
    *      java.lang.Throwable)
    */
   public ILogData crearLog(String mensaje, boolean resBundle, int nivel, int tipo, String ficheroConf, String usuario, Throwable e) {
      Level nlog = null;
      if (ILogData.INFO == nivel) {
         nlog = Level.INFO;
      } else if (ILogData.WARN == nivel) {
         nlog = Level.WARN;
      } else if (ILogData.OFF == nivel) {
         nlog = Level.OFF;
      } else if (ILogData.DEBUG == nivel) {
         nlog = Level.DEBUG;
      } else if (ILogData.ERROR == nivel) {
         nlog = Level.ERROR;
      } else {
         nlog = Level.OFF;
      }

      String claseLlamante;
      if (e != null) {
         claseLlamante = e.getStackTrace()[0].getClassName();
         logger.log(claseLlamante, nlog, mensaje, e);
      } else {
         logger.log(nlog, mensaje);
      }

      return null;
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.psiject.utilidades.ILogService#crearLog(java.lang.String,
    *      boolean, int, java.lang.String, java.lang.Throwable)
    */
   public ILogData crearLog(String mensaje, boolean resBundle, int nivel, String realPath, Throwable e) {
      crearLog(mensaje, resBundle, nivel, -1, "", "", e);
      return null;
   }

}
